/*
 * @file     g_i2c.h
 * @brief    This file provides all the I2C firmware functions.
 *
 * Change Logs:
 * Date           Author             Version        Notes
 * 2021-05-14     dengzhiqian        V1.0.0         the first version
 */

#ifndef __G_I2C_H__
#define __G_I2C_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "g_1903.h"
#include "g_system.h"

/** @defgroup I2C_R\W_FUNC
 * @{
 */
#define IICx MI2C0
#define I2C_WRITE_FUNC  0
#define I2C_READ_FUNC   1

#define IS_I2C(I2Cx) ((I2Cx == MI2C0) || I2Cx == MI2C1)

/**
  *@brief I2C ClockSpeed define
  */

/*I2C_ClockSpeed_100KHz*/
#if 1/*100KHZ GKT modify*/
#define SCLL_100KHZ     (232)
#define SCLH_100KHZ     (232)
#define STSU_100KHZ     (116)
#define STHD_100KHZ     (116)
#define SOSU_100KHZ     (116)
#define DTSU_100KHZ     (116)
#define DTHD_100KHZ     (0)
#else/*200KHZ*/
#define SCLL_100KHZ     (116)
#define SCLH_100KHZ     (116)
#define STSU_100KHZ     (58)
#define STHD_100KHZ     (58)
#define SOSU_100KHZ     (58)
#define DTSU_100KHZ     (58)
#define DTHD_100KHZ     (0)
#endif
/*I2C_ClockSpeed_400KHz*/

#define SCLL_400KHZ     (26)
#define SCLH_400KHZ     (26)
#define STSU_400KHZ     (12)
#define STHD_400KHZ     (12)
#define SOSU_400KHZ     (12)
#define DTSU_400KHZ     (24)
#define DTHD_400KHZ     (0)

/*I2C_ClockSpeed_800KHz*/

#define SCLL_800KHZ     (11)
#define SCLH_800KHZ     (11)
#define STSU_800KHZ     (6)
#define STHD_800KHZ     (6)
#define SOSU_800KHZ     (6)
#define DTSU_800KHZ     (11)
#define DTHD_800KHZ     (0)

/*I2C_ClockSpeed_1.2MHz*/

#define SCLL_1_2MHZ     (6)
#define SCLH_1_2MHZ     (6)
#define STSU_1_2MHZ     (3)
#define STHD_1_2MHZ     (3)
#define SOSU_1_2MHZ     (3)
#define DTSU_1_2MHZ     (6)
#define DTHD_1_2MHZ     (0)

/**
  *@brief I2C Core parameters struct
  */
typedef struct
{
    uint8_t scll; /*!< Specifies the Clock Pulse Width Low.  */
    uint8_t sclh; /*!< Specifies the Clock Pulse Width High. */
    uint8_t stsu; /*!< Specifies the Start Setup Time.       */
    uint8_t sthd; /*!< Specifies the Start Hold Time.        */
    uint8_t sosu; /*!< Specifies the Stop Setup Time.        */
    uint8_t dtsu; /*!< Specifies the Data Setup Time.        */
    uint8_t dthd; /*!< Specifies the Data Hold Time.         */
} G_I2C_InitTypeDef;

void G_I2C_DeInit(I2C_TypeDef *I2Cx);
void G_I2C_Init(I2C_TypeDef *I2Cx, G_I2C_InitTypeDef *I2C_InitStruct);
void G_I2C_ClearITPendingBit(I2C_TypeDef *I2Cx);
void G_I2C_ITConfig(I2C_TypeDef *I2Cx, G_FunctionalState NewState);
G_ITStatus G_I2C_GetITStatus(I2C_TypeDef *I2Cx);
void G_I2C_SendData(I2C_TypeDef *I2Cx, uint8_t data);
void G_I2C_SendBytes(I2C_TypeDef *I2Cx, uint8_t *Src, uint32_t len);
uint8_t I2C_ReceiveData(I2C_TypeDef *I2Cx);
void G_I2C_ReceiveBytes(I2C_TypeDef *I2Cx, uint8_t *Src, uint32_t Srclen, uint8_t *Dest, uint32_t Destlen);

#ifdef __cplusplus
}
#endif

#endif

/************************ (C) COPYRIGHT GKT *****END OF FILE****/
